package artid.com.arttogoa.adapter;

import java.io.File;
import java.text.DecimalFormat;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import artid.com.arttogoa.R;
import artid.com.arttogoa.db.SDCache;
import artid.com.arttogoa.parser.FilesBean;
import artid.com.arttogoa.task.CallbackSky;
import com.bumptech.glide.Glide;

public class GridAdapter extends BaseAdapter {

	private List<FilesBean> filesList;
	private Context mContext;
	// ImageLoader mImageLoader;
	private int type;
	private GridView mgridView;
	private boolean bo;
	private CallbackSky cs;

	public GridAdapter(Context context, List<FilesBean> filesList, int type,
			GridView mgridView, boolean bo, CallbackSky cs) {
		if (cs != null)
			this.cs = cs;
		this.mgridView = mgridView;
		this.bo = bo;
		this.type = type;
		this.filesList = filesList;
		this.mContext = context;
		// RequestQueue mQueue = Volley.newRequestQueue(context);
		// mImageLoader = new ImageLoader(mQueue, new BitmapCache());
	}

	@Override
	public int getCount() {
		return bo ? filesList.size() + 1 : filesList.size();
	}

	@Override
	public Object getItem(int position) {
		return filesList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			vh = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_grid, null);
			vh.back = (RelativeLayout) convertView.findViewById(R.id.rel_back);
			vh.text = (TextView) convertView.findViewById(R.id.text);
			vh.iv = (ImageView) convertView.findViewById(R.id.icon);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		FilesBean item = null;
		String url = "";

		if (type == SDCache.WIDISD) {
			if (bo & position == 0) {
				vh.back.setVisibility(View.VISIBLE);
				vh.text.setVisibility(View.GONE);
				vh.iv.setVisibility(View.GONE);
				vh.back.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						mgridView.setVisibility(View.GONE);
					}
				});
				return convertView;
			} else {
				vh.back.setVisibility(View.GONE);
				vh.text.setVisibility(View.VISIBLE);
				vh.iv.setVisibility(View.VISIBLE);
				if (bo)
					item = filesList.get(position - 1);
				else
					item = filesList.get(position);
				url = item.getThumbURL();
				// ImageListener listener = ImageLoader.getImageListener(vh.iv,
				// android.R.drawable.ic_menu_rotate,
				// android.R.drawable.ic_delete);
				// mImageLoader.get(url, listener);
				Glide.with(mContext).load(url)
						.placeholder(R.drawable.yspba_img_mor)
						.error(android.R.drawable.ic_delete).into(vh.iv);
			}
		} else {
			item = filesList.get(position);
			url = item.getThumbURL();
			// vh.iv.setTag(url);
			Glide.with(mContext).load(new File(url))
					.placeholder(R.drawable.yspba_img_mor)
					.error(android.R.drawable.ic_delete).into(vh.iv);
			vh.iv.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					if (cs != null)
						cs.callBack(position);
				}
			});
			// 利用NativeImageLoader类加载本地图片
			// Bitmap bitmap = NativeImageLoader.getInstance().loadNativeImage(
			// url, new NativeImageCallBack() {
			//
			// @Override
			// public void onImageLoader(Bitmap bitmap, String path) {
			// ImageView mImageView = (ImageView) mgridView
			// .findViewWithTag(path);
			// if (bitmap != null && mImageView != null) {
			// mImageView.setImageBitmap(bitmap);
			// }
			// }
			// });
			// if (bitmap != null) {
			// vh.iv.setImageBitmap(bitmap);
			// }
		}
		if (!TextUtils.isEmpty(item.getFileSize())) {
			int size = Integer.parseInt(item.getFileSize());
			int kb = size / 1024;
			DecimalFormat df = new DecimalFormat("###.00");
			vh.text.setText(kb < 1024 ? (kb + "KB")
					: (df.format(kb / 1024f) + "M"));
		}

		return convertView;
	}

	class ViewHolder {
		TextView text;
		ImageView iv;
		RelativeLayout back;
	}
}